<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Worker\Controller;
use Think\Model;
use Worker\Logic\WorkerLogic;

/**
 * 控制器
 * @author derek <693472824@qq.com>
 */
class WorkerController extends BaseController {
    
    /**
     * 转交时根据工号获取其他小哥信息
     */
    public function search(){
        
        $account = I('POST.account');
        $token = I('POST.token');
        
        if(!$this->checkToken($account, $token)){
        
            $this->rsp['rspCode'] = '405';
            $this->rsp['rspDesc'] = '权限判断失败';
            exit(json_encode($this->rsp));
        }
        
        $eid = I('POST.eid');
        $workerID = WorkerLogic::getIDByAccount($account);
        
        $map['worker_id'] = array('NEQ', $workerID);
        $map['eid'] = (string)$eid;
        
        $view = D('Api/WorkerView');
        $worker = $view->where($map)->field('account,name,eid,job,profession,sex,score,avg_hour,orders_sum,fullpath')->find();
        
        if(!empty($worker)){
            
            $worker['job_name'] = get_dictionary_enum_name('job_options', $worker['job']);
            $worker['profession_name'] = get_dictionary_enum_name('profession_options', $worker['profession']);
            
            $this->rsp['rspCode'] = '200';
            $this->rsp['rspCont'] = $worker;
        }else{
            
            $this->rsp['rspCode'] = '302';
            $this->rsp['rspDesc'] = '未找到数据';
        }
        
        exit(json_encode($this->rsp));
    }
    
    /**
     * 订单
     */
    public function statistics(){
        
        $account = I('account');
        $token = I('POST.token');
        
        
        $orderModel = M('Orders');
        $workerModel = M('Worker');
        
        $workerID = WorkerLogic::getIDByAccount($account);
        $worker = $workerModel->where(array('worker_id'=>$workerID))->find();
        $map['worker_id'] = $workerID;
        
        //待服务
        $map['status'] = array('in', '2,3');
        $statistics[] = $orderModel->where($map)->Count('order_id');
        
        //待支付
        $map['status'] = array('in', '4,5,10');
        $statistics[] = $orderModel->where($map)->Count('order_id');
        
        //已完成（6：已完成，待评价；9：已评价）
        $map['status'] = array('in', '6,9');
        $statistics[] = $orderModel->where($map)->Count('order_id');
        
        //退单
        $map['status'] = '7';
        $statistics[] = $orderModel->where($map)->Count('order_id');
        
        //统计当前年份接单数
        $initTime = strtotime(date('Y-01-01', time()));
        $statisticsMap['start_time'] = array('GT', $initTime);
        $statisticsMap['status'] = array('egt', '3');
        $statisticsMap['worker_id'] = array('egt', '1');
        $statisticsMap['site_id'] = array('egt', '1');
        
        //全站最高接单数、最低工时数
        $count = $orderModel->where($statisticsMap)->group('worker_id')->order('tp_count DESC')->count('order_id');
        $order[] = empty($count) ? '0' : $count;
        $avg = $orderModel->where($statisticsMap)->group('worker_id')->order('tp_avg')->avg('service_time');
        $hour[] = round($avg/60, 1);
        
        //本站最高接单数、最低工时数
        $statisticsMap['site_id'] = $worker['site_id'];
        $count = $orderModel->where($statisticsMap)->group('worker_id')->order('tp_count DESC')->count('order_id');
        $order[] = empty($count) ? '0' : $count;
        $avg = $orderModel->where($statisticsMap)->group('worker_id')->order('tp_avg')->avg('service_time');
        $hour[] = round($avg/60, 1);
        
        //个人接单数、工时数
        unset($statisticsMap['site_id']);                   //小哥调换站点后，原业绩也在统计范围之内
        $statisticsMap['worker_id'] = $workerID;
        $count = $orderModel->where($statisticsMap)->count('order_id');
        $order[] = empty($count) ? '0' : $count;
        $avg = $orderModel->where($statisticsMap)->avg('service_time');
        $hour[] = round($avg/60, 1);
        
        //个人接单数、平均工时
        $info[] = $worker['orders_sum'];           //接单数为实时更新
        $info[] = $worker['avg_hour'];
        
        $rspCont['statistics'] = $statistics;
        $rspCont['order'] = $order;
        $rspCont['hour'] = $hour;
        $rspCont['info'] = $info;
        $rspCont['score'] = $worker['score'];
        
        $this->rsp['rspCode'] = '200';
        $this->rsp['rspCont'] = $rspCont;
        
        exit(json_encode($this->rsp));
    }
    
    /**
     * 修改信息
     * @author derek <693472824@qq.com>
     */
    public function updateInfo(){
    
        $account = I('POST.account');
        $token = I('POST.token');
    
        if(!$this->checkToken($account, $token)){
    
            $this->rsp['rspCode'] = '405';
            $this->rsp['rspDesc'] = '权限验证失败';
            exit(json_encode($this->rsp));
        }
    
        $name = I('POST.name');
        $sex = I('POST.sex');
        $birthday = I('POST.birthday');
        $job = I('POST.job');
    
        if(!empty($name)){
            $data['name'] = (string)$name;
        }
        if(in_array($sex, array('1', '2'))){
            $data['sex'] = $sex;
        }
        if(!empty($birthday)){
            $data['birthday'] = GetMkTime($birthday);
        }
        if(!empty($job)){
            $data['job'] = $job;
        }
    
        $map['account'] = $account;
    
        $workerModel = D('Worker');
    
        if($workerModel->create($data) && $workerModel->where($map)->save()){
    
            $this->rsp['rspCode'] = '200';
        }else{
    
            $this->rsp['rspCode'] = '301';
            $this->rsp['rspDesc'] = '操作数据库异常'.$workerModel->getError();
        }
    
        exit(json_encode($this->rsp));
    }
    
    /**
     * 已登录用户上传头像
     *
     * @author derek <lixm@my818.com>
     */
    public function updatePhoto(){
    
        $account = I('POST.account');
        $token = I('POST.token');
    
        if(!$this->checkToken($account, $token)){
    
            $this->rsp['rspCode'] = '422';
            $this->rsp['rspDesc'] = '参数异常';
            exit(json_encode($this->rsp));
        }
    
        $file_driver = C('UPLOAD_DRIVER');
    
        $attLogic = D('Blovex4/Attachment', 'Logic');
    
        $info = $attLogic->upload(
                $_FILES,
                C('UPLOAD_PICTURE_CONFIG'),
                $file_driver,
                C("UPLOAD_{$file_driver}_CONFIG")
        );
    
        /* 记录附件信息 */
        if($info){
    
            $attID = $info['photo']['att_id'];
    
            $map['account'] = $account;
    
            $workerModel = M('Worker');
    
            if($workerModel->where($map)->setField('att_id', $attID)){
    
                $data['photo'] = $info['photo']['fullpath'];
    
                $this->rsp['rspCode'] = '200';
                $this->rsp['rspCont'] = $data;
            }else{
    
                $this->rsp['rspCode'] = '301';
                $this->rsp['rspDesc'] = '操作数据库异常'.$workerModel->getError();
            }
        } else {
            $this->rsp['rspCode'] = '115';
            $this->rsp['rspDesc'] = '上传失败'.$attLogic->getError();
        }
    
        exit(json_encode($this->rsp));
    }
    
    /**
     * 更换手机账号
     * @author derek <lixm@my818.com>
     */
    public function updateAccount(){
    
        $account = I('POST.account');
        $token = I('POST.token');
        $validation = I('POST.validation');
        $telphone = I('POST.telphone');
    
        if(!$this->checkToken($account, $token)){
    
            $this->rsp['rspCode'] = '405';
            $this->rsp['rspDesc'] = '权限验证失败';
            exit(json_encode($this->rsp));
        }elseif (!checkMobile($telphone)){
            $this->rsp['rspCode'] = '422';
            $this->rsp['rspDesc'] = '参数异常';
            exit(json_encode($this->rsp));
        }
    
        //账号-验证码信息
        $validateInfo = S('validate.'.$telphone);
        
        if(!is_array($validateInfo) || $validation <> $validateInfo['validation']){
            $this->rsp['rspCode'] = '103';
            $this->rsp['rspDesc'] = '验证码错误';
            exit(json_encode($this->rsp));
        }
        
        $data['account'] = $telphone;
        $data['telphone'] = $telphone;
        
        $workerModel = D('Worker');
        
        if($workerModel->create($data)){
            $result = $workerModel->where(array('account'=>$account))->save();
            
            if($result !== false){
                S('token.'.$account, null);
                $this->rsp['rspCode'] = '200';
            }else{
                $this->rsp['rspCode'] = '301';
                $this->rsp['rspDesc'] = '操作数据库异常';
            }
        }else{
            $this->rsp['rspCode'] = '303';
            $this->rsp['rspDesc'] = '操作失败'.$workerModel->getError();
        }
    
        exit(json_encode($this->rsp));
    }
    

    /**
     * 意见反馈
     * @author derek <693472824@qq.com>
     */
    public function feedback(){
    
        $account = I('POST.account');
        $token = I('POST.token');
    
        if(!$this->checkToken($account, $token)){
    
            $this->rsp['rspCode'] = '405';
            $this->rsp['rspDesc'] = '权限验证失败';
            exit(json_encode($this->rsp));
        }
    
        $workerID = WorkerLogic::getIDByAccount($account);
        $data['content'] = I('POST.content');
        $data['user_id'] = $workerID;
        $data['user_type'] = '2';
    
        $feedback = D('Feedback');
    
        if($feedback->create($data) && $feedback->add()){
    
            $this->rsp['rspCode'] = '200';
        }else{
    
            $this->rsp['rspCode'] = '422';
            $this->rsp['rspDesc'] = '参数异常'.$feedback->getError();
        }
    
        exit(json_encode($this->rsp));
    }
}
